<template>
  <div class="app-container" style="position: relative">
    <el-tabs v-model="releaseType" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane
        v-if="!(editData.content_id && editData.draft_status == 3)"
        :label="!Object.keys(editData).length ? `创建方案` : `编辑方案`"
        :name="0"
      >
        <add-edit
          v-if="!releaseType"
          :data="editData"
          :releaseType="releaseType"
          @reset-edit-data="resetEditData"
        />
      </el-tab-pane>
      <el-tab-pane
        v-if="!(editData.content_id && !editData.draft_status)"
        :label="!Object.keys(editData).length ? `创建模板` : `编辑模板`"
        :name="1"
      >
        <add-edit
          v-if="releaseType"
          :data="editData"
          :releaseType="releaseType"
          @reset-edit-data="resetEditData"
        />
      </el-tab-pane>
    </el-tabs>
    <div style="position: absolute; top: 20px; right: 30px">
      <el-button
        size="large"
        @click="
          () => {
            returnPage()
          }
        "
        >返回</el-button
      >
    </div>
  </div>
</template>

<script>
import AddEdit from './cps/add-edit.vue'
import defaultSettings from '@/settings'

export default {
  name: 'AddEditView',
  components: {
    AddEdit
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      releaseType: 0,
      editData: {}
    }
  },
  created() {
    const storePrefix = defaultSettings.storePrefix
    const editData = localStorage.getItem(storePrefix + 'editData')
    this.editData = JSON.parse(editData)
    if (this.editData.draft_status == 3) {
      this.releaseType = 1
    } else {
      this.releaseType = 0
    }
    console.log(JSON.parse(editData))
  },
  methods: {
    resetEditData() {
      const storePrefix = defaultSettings.storePrefix
      this.editData = {}
      localStorage.setItem(storePrefix + 'editData', JSON.stringify({}))
    },
    returnPage() {
      this.$router.replace({path: '/article/index', query: this.$route.query})
    }
  }
}
</script>

<style lang="scss" scoped></style>
